/**
 * Copyright 2009. Felix Dorner.
 * This code is licensed under the Eclipse Public License v1.0
 * http://www.eclipse.org/org/documents/epl-v10.html
 *
 * $Id$
 */
package com.objectcarpentry.kaylee.impl;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;

import com.objectcarpentry.kaylee.FilteredPlaylist;
import com.objectcarpentry.kaylee.AbstractPlaylist;
import com.objectcarpentry.kaylee.IPlayableFilter;
import com.objectcarpentry.kaylee.KayleePackage;
import com.objectcarpentry.kaylee.Playable;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Abstract Filtered Playlist</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.objectcarpentry.kaylee.impl.FilteredPlaylistImpl#getFilter <em>Filter</em>}</li>
 *   <li>{@link com.objectcarpentry.kaylee.impl.FilteredPlaylistImpl#getDelegate <em>Delegate</em>}</li>
 *   <li>{@link com.objectcarpentry.kaylee.impl.FilteredPlaylistImpl#getElements <em>Elements</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class FilteredPlaylistImpl extends AbstractPlaylistImpl<Playable> implements FilteredPlaylist {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static final String copyright = "Copyright 2009. Felix Dorner.\nThis code is licensed under the Eclipse Public License v1.0\nhttp://www.eclipse.org/org/documents/epl-v10.html";

	/**
	 * The default value of the '{@link #getFilter() <em>Filter</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFilter()
	 * @generated
	 * @ordered
	 */
	protected static final IPlayableFilter FILTER_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getFilter() <em>Filter</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFilter()
	 * @generated
	 * @ordered
	 */
	protected IPlayableFilter filter = FILTER_EDEFAULT;

	/**
	 * The cached value of the '{@link #getDelegate() <em>Delegate</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDelegate()
	 * @generated
	 * @ordered
	 */
	protected AbstractPlaylist<? extends Playable> delegate;

	/**
	 * The cached value of the '{@link #getElements() <em>Elements</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getElements()
	 * @generated
	 * @ordered
	 */
	protected EList<Playable> elements;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected FilteredPlaylistImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return KayleePackage.Literals.FILTERED_PLAYLIST;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public IPlayableFilter getFilter() {
		return filter;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFilter(IPlayableFilter newFilter) {
		IPlayableFilter oldFilter = filter;
		filter = newFilter;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.FILTERED_PLAYLIST__FILTER, oldFilter, filter));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	public AbstractPlaylist<? extends Playable> getDelegate() {
		if (delegate != null && delegate.eIsProxy()) {
			InternalEObject oldDelegate = (InternalEObject)delegate;
			delegate = (AbstractPlaylist<? extends Playable>)eResolveProxy(oldDelegate);
			if (delegate != oldDelegate) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, KayleePackage.FILTERED_PLAYLIST__DELEGATE, oldDelegate, delegate));
			}
		}
		return delegate;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public AbstractPlaylist<? extends Playable> basicGetDelegate() {
		return delegate;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setDelegate(AbstractPlaylist<? extends Playable> newDelegate) {
		AbstractPlaylist<? extends Playable> oldDelegate = delegate;
		delegate = newDelegate;
	
		ArrayList<Playable> tmp = new ArrayList<Playable> (delegate.getElements().size());
		for (Playable p : delegate.getElements()){
			if (p.match(getFilter())){
				
				tmp.add(p);
			}
		}
		
		getElements().clear();
		getElements().addAll(tmp);
		
		
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.FILTERED_PLAYLIST__DELEGATE, oldDelegate, delegate));
	}

	

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Playable> getElements() {
		if (elements == null) {
			elements = new EObjectResolvingEList<Playable>(Playable.class, this, KayleePackage.FILTERED_PLAYLIST__ELEMENTS);
		}
		return elements;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case KayleePackage.FILTERED_PLAYLIST__FILTER:
				return getFilter();
			case KayleePackage.FILTERED_PLAYLIST__DELEGATE:
				if (resolve) return getDelegate();
				return basicGetDelegate();
			case KayleePackage.FILTERED_PLAYLIST__ELEMENTS:
				return getElements();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case KayleePackage.FILTERED_PLAYLIST__FILTER:
				setFilter((IPlayableFilter)newValue);
				return;
			case KayleePackage.FILTERED_PLAYLIST__DELEGATE:
				setDelegate((AbstractPlaylist<? extends Playable>)newValue);
				return;
			case KayleePackage.FILTERED_PLAYLIST__ELEMENTS:
				getElements().clear();
				getElements().addAll((Collection<? extends Playable>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case KayleePackage.FILTERED_PLAYLIST__FILTER:
				setFilter(FILTER_EDEFAULT);
				return;
			case KayleePackage.FILTERED_PLAYLIST__DELEGATE:
				setDelegate((AbstractPlaylist<? extends Playable>)null);
				return;
			case KayleePackage.FILTERED_PLAYLIST__ELEMENTS:
				getElements().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case KayleePackage.FILTERED_PLAYLIST__FILTER:
				return FILTER_EDEFAULT == null ? filter != null : !FILTER_EDEFAULT.equals(filter);
			case KayleePackage.FILTERED_PLAYLIST__DELEGATE:
				return delegate != null;
			case KayleePackage.FILTERED_PLAYLIST__ELEMENTS:
				return elements != null && !elements.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (filter: ");
		result.append(filter);
		result.append(')');
		return result.toString();
	}

	@Override
	public String getName() {
		return delegate == null ? "" : delegate.getName();
	}

	@Override
	public boolean match(IPlayableFilter filter) {
		return false;
	}

} //AbstractFilteredPlaylistImpl
